package com.sino.config.server.service;

import java.util.ArrayList;
import java.util.List;

import org.nutz.lang.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sino.config.server.model.Config;
import com.sino.config.server.model.PropertySource;
import com.sino.config.server.service.config.PropertyService;

/**
 * @author kerbores
 *
 */
@Service
public class ConfigService {

	@Autowired
	PropertyService propertyService;

	/**
	 * @param label
	 *            分支 label
	 * @param profile
	 *            profile 信息
	 * @param application
	 *            应用名称
	 * 
	 */
	public Config loadConfig(String application, String profile, String label) {
		Config config = new Config();
		config.setName(application);
		config.setLabel(label);
		config.setProfiles(Lang.list(profile));
		List<PropertySource> propertySources = new ArrayList<PropertySource>();

		PropertySource propertySource = new PropertySource();
		propertySource.setName(profile);
		propertySource.setSource(propertyService.show(application, profile, label).setv("spring.application.name", application));// 覆盖应用名称

		propertySources.add(propertySource);
		config.setPropertySources(propertySources);
		return config;
	}
}
